//
//  DCChangeBankSmsView.m
//  Demo
//
//  Created by zhaolin on 2023/1/6.
//

#import "DCChangeBankSmsView.h"

#define TotltTime 60

@interface DCChangeBankSmsView ()
@property (nonatomic, strong) UITextField *vcodeTF;

@property (nonatomic, strong) UILabel *vcodeSendLabel;

@property (nonatomic, strong) UILabel *statusLabel;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) NSInteger timeNumber;

@end
@implementation DCChangeBankSmsView

- (NSTimer *)timer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;;
}

- (NSString *)vCode {
    return _vcodeTF.text;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configureUI];
        _timeNumber = TotltTime;

    }
    return self;
}

- (void)configureUI {
    
    DCUserInfo *userInfo = [[CacheTools sharedCacheTools] getObjForKey:SMARTUSERINFO];
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(306);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"icon_close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
        make.width.height.mas_equalTo(50);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"Currently logged in account";
    titleLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    titleLabel.font = DCFont_PF_Medium(14);
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(36);
        make.top.mas_equalTo(39);
    }];
    
    UILabel *phoneNumLabel = [UILabel new];
    phoneNumLabel.text = [NSString stringWithFormat:@"+234 %@",userInfo.phone];
    phoneNumLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    phoneNumLabel.font = DCFont_PF_Semibold(14);
    [bgView addSubview:phoneNumLabel];
    [phoneNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(38);
        make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(42);
    }];
    
    UILabel *vcodeSendLabel = [UILabel new];
    _vcodeSendLabel = vcodeSendLabel;
    vcodeSendLabel.textAlignment = NSTextAlignmentCenter;
    vcodeSendLabel.font = DCFont_PF_Semibold(14);
    vcodeSendLabel.textColor = [UIColor colorWithString:@"#FFFFFF"];
    vcodeSendLabel.backgroundColor = [UIColor colorWithString:@"#C7C7C7"];
    vcodeSendLabel.layer.cornerRadius = 4;
    vcodeSendLabel.layer.masksToBounds = YES;
    vcodeSendLabel.text = @"send";
    vcodeSendLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapSend = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSendAction:)];
    [vcodeSendLabel addGestureRecognizer:tapSend];
    [bgView addSubview:vcodeSendLabel];
    [vcodeSendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-41);
        make.height.mas_equalTo(48);
        make.top.mas_equalTo(phoneNumLabel.mas_bottom).mas_offset(20);
        make.width.mas_equalTo(104);
    }];
    
    UIView *vcodeView = [UIView new];
    vcodeView.layer.cornerRadius = 4;
    vcodeView.layer.masksToBounds = YES;
    vcodeView.layer.borderColor = [[UIColor colorWithString:@"#E5E5E5"] CGColor];
    vcodeView.layer.borderWidth = 1;
    vcodeView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [bgView addSubview:vcodeView];
    [vcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(41);
        make.top.mas_equalTo(phoneNumLabel.mas_bottom).mas_offset(20);
        make.height.mas_equalTo(48);
        make.right.mas_equalTo(vcodeSendLabel.mas_left).mas_offset(-11);
    }];
    
    UITextField *vcodeTF = [UITextField new];
    _vcodeTF = vcodeTF;
#ifdef DEBUG
    vcodeTF.text = @"0000";
#else
    vcodeTF.text = @"";
#endif
    vcodeTF.textColor = [UIColor colorWithString:@"#1C221B"];
    vcodeTF.font = DCFont_PF_Semibold(14);
    NSAttributedString *placeH = [[NSAttributedString alloc] initWithString:@"Verification code" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithString:@"#C7C7C7"],NSFontAttributeName:DCFont_PF_Regular(12)}];
    vcodeTF.attributedPlaceholder = placeH;
    [vcodeView addSubview:vcodeTF];
    [vcodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
        make.right.mas_equalTo(-35);
        make.centerY.mas_equalTo(vcodeView);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *statusLabel = [UILabel new];
    statusLabel.text = @"*Wrong verification code";
    statusLabel.textColor = [UIColor colorWithString:@"#FF5252"];
    statusLabel.font = DCFont_PF_Regular(12);
    _statusLabel = statusLabel;
    statusLabel.hidden = YES;
    [bgView addSubview:statusLabel];
    [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(vcodeView.mas_bottom).mas_offset(5);
    }];
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.backgroundColor = [UIColor colorWithString:@"#1C221B"];
    commitBtn.layer.cornerRadius =8;
    commitBtn.layer.masksToBounds = YES;
    [commitBtn setTitle:@"Submit" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    commitBtn.titleLabel.font = DCFont_PF_Semibold(15);
    [commitBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(38);
        make.right.mas_equalTo(-38);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(vcodeView.mas_bottom).mas_offset(30);
    }];
    
}
- (void)sendCodel {
    //register login
    [MBProgressHUD showHUDAddedTo:self animated:YES];
    DCUserInfo *userInfo = [[CacheTools sharedCacheTools] getObjForKey:SMARTUSERINFO];
    [DCHttpRequest DCgetSmsWithPhoneNum:userInfo.phone registerType:@"bank" completeBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self animated:YES];
        if (responseObject.code == 200) {
            [self.vcodeTF becomeFirstResponder];
            [self.timer fire];
            //【SMS verification code obtained successfully】
            [DCDCToast toastText:@"【SMS verification code obtained successfully】"];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];

}

//已发送
- (void)sendingVCodel {
    [self.vcodeTF becomeFirstResponder];
    [self.timer fire];
}

- (void)timerAction {
    if (_timeNumber <= 0) {
        self.timeNumber = TotltTime;
        self.vcodeSendLabel.text = @"send";
        self.vcodeSendLabel.userInteractionEnabled = YES;
        [self.timer invalidate];
        self.timer = nil;
    } else {
        self.timeNumber --;
        self.vcodeSendLabel.text = [NSString stringWithFormat:@"%ld",_timeNumber];
        self.vcodeSendLabel.userInteractionEnabled = NO;
    }
}


- (void)closeAction:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeBankSmsViewCloseAction:)]) {
        [self.delegate changeBankSmsViewCloseAction:self];
    }
}
- (void)commitAction:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeBankSmsViewCommitActionwithText:view:)]) {
        [self.delegate changeBankSmsViewCommitActionwithText:@"" view:self];
    }
}
- (void)tapSendAction:(UITapGestureRecognizer *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeBankSmsViewSendCodeAction:)]) {
        [self.delegate changeBankSmsViewSendCodeAction:self];
    }
//    [self sendCodel];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
